package com.itkjb.leisurely.resource.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itkjb.leisurely.core.base.service.impl.TierBaseServiceImpl;
import com.itkjb.leisurely.core.model.PageModel;
import com.itkjb.leisurely.core.model.SearchModel;
import com.itkjb.leisurely.core.util.RedisUtil;
import com.itkjb.leisurely.core.util.TreeUtils;
import com.itkjb.leisurely.resource.common.SysConstants;
import com.itkjb.leisurely.resource.sys.mapper.SysDictGroupMapper;
import com.itkjb.leisurely.resource.sys.model.SysDictGroupModel;
import com.itkjb.leisurely.resource.sys.model.vo.SysDictGroupVO;
import com.itkjb.leisurely.resource.sys.service.ISysDictGroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * 字典分组 服务实现类
 * </p>
 *
 * @author itkjb.com
 * @since 2020-08-14
 */
@Service
public class SysDictGroupServiceImpl extends TierBaseServiceImpl<SysDictGroupMapper, SysDictGroupModel> implements ISysDictGroupService {

    @Autowired
    RedisUtil redisUtil;
    private static final String SUB_IDS_KEY = SysConstants.SYS_PREFIX + SysConstants.DICT_GROUP_KEY + "sub_ids";

    @Override
    public boolean save(SysDictGroupModel entity) {
        this.clearCatch();
        return super.save(entity);
    }

    @Override
    public boolean updateById(SysDictGroupModel entity) {
        this.clearCatch();
        return super.updateById(entity);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        this.clearCatch();
        return super.removeByIds(idList);
    }

    /**
     * 字典分组结构树
     * @param searchModel
     * @return
     */
    @Override
    public List<SysDictGroupVO> tree(SearchModel<SysDictGroupModel> searchModel) {
        List<SysDictGroupModel> list = super.list(searchModel);
        List<SysDictGroupVO> voList = new ArrayList<>();
        if(list !=null && list.size()>0 ){
            for (SysDictGroupModel sysDictGroupModel : list) {
                SysDictGroupVO vo = new SysDictGroupVO();
                BeanUtils.copyProperties(sysDictGroupModel, vo);
                voList.add(vo);
            }
            TreeUtils<SysDictGroupVO> treeUtil = new TreeUtils<>();
            voList = treeUtil.tree(voList);
        }
       return voList;
    }

    /**
     * 获取指定分组的所有下级id
     * @param id
     * @return
     */
    @Override
    public Set<String> getSubIds(String id) {
        Set<String> vlist = null;
        Map<String, Set<String>> subIdSMap = redisUtil.hmget(SUB_IDS_KEY);
        if (subIdSMap == null || subIdSMap.size() <= 0) {
            SearchModel<SysDictGroupModel> searchModel = new SearchModel<>();
            searchModel.setModel(new SysDictGroupModel());
            searchModel.setPageModel(new PageModel<>());
            List<SysDictGroupVO> treeList = tree(searchModel);
            TreeUtils<SysDictGroupVO> treeUtils = new TreeUtils<>();
            subIdSMap = treeUtils.getSubIds(treeList, SysConstants.DEFAULT_TOP_PARENT_ID);
            redisUtil.hmset(SUB_IDS_KEY, subIdSMap, SysConstants.TIME_120_SECOND * 2);
        }
        vlist = subIdSMap.get(id);
        return vlist;
    }

    /**
     * 清除缓存
     */
    protected void clearCatch() {
        redisUtil.del(SUB_IDS_KEY);
    }

    /**
     * wrapper 组装器，构造条件查询
     * @author itkjb.com
     * @date  2020-08-14
     * @param wrapper
     * @param model 条件
     **/
    @Override
    protected void assembleWrapper(QueryWrapper<SysDictGroupModel> wrapper, SysDictGroupModel model) {
        // （代码生成器生成）自动生成了，list查询的时候，条件组装，开发者自行删除不需要的条件
        // 分组名称
        wrapper.lambda().like(StringUtils.isNotBlank(model.getName()), SysDictGroupModel::getName, model.getName() );
        // 父级id
        wrapper.lambda().eq(StringUtils.isNotBlank(model.getParentId()), SysDictGroupModel::getParentId, model.getParentId() );
    }

    /**
     * wrapper 组装器，组装list查询时，查询的列
     * @author itkjb.com
     * @date  2020-08-14
     * @param wrapper
     * @param model
     * @return void
     * @since 2020-08-14
     **/
    @Override
    protected void buildListSelectColumns(QueryWrapper<SysDictGroupModel> wrapper, SysDictGroupModel model){
        buildCommonSelectColumns(wrapper, model);
    }

    /**
     * 组装列表查询的列（字段）
     * @date  2020-08-14
     * @param wrapper
     * @param model
     */
    protected void buildCommonSelectColumns(QueryWrapper<SysDictGroupModel> wrapper, SysDictGroupModel model){
        // 代码生成器生成，为了简化开发时，查询list时，查询指定字段，没必要查询所有参数
        wrapper.lambda().select(
            // id
            SysDictGroupModel::getId,
            // 分组名称
            SysDictGroupModel::getName,
            // 父级id
            SysDictGroupModel::getParentId,
            // 父级名称
            SysDictGroupModel::getParentName,
            // 下级数量
            SysDictGroupModel::getSubCount,
            // 排序
            SysDictGroupModel::getPaixu,
            // 状态  0：禁用 ，1： 可用
            SysDictGroupModel::getStatus,
            SysDictGroupModel::getCreateTime
        );
    }


}
